package com.mall.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.mapper.*;
import com.mall.model.domain.*;
import com.mall.model.dto.ProductDto;
import com.mall.model.vo.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private PmsMemberPriceMapper pmsMemberPriceMapper;
    @Autowired
    private CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper;
    @Autowired
    private CmsPrefrenceAreaProductRelationMapper cmsPrefrenceAreaProductRelationMapper;
    @Autowired
    private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;
    @Autowired
    private PmsProductCategoryAttributeRelationMapper pmsProductCategoryAttributeRelationMapper;
    @Autowired
    private PmsProductFullReductionMapper pmsProductFullReductionMapper;
    @Autowired
    private PmsProductLadderMapper pmsProductLadderMapper;
    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private PmsProductVertifyRecordMapper pmsProductVertifyRecordMapper;

    /**
     * 创建商品
     *
     * @param productDto
     */
    public void save(ProductDto productDto) {
        //保存商品基础表
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(productDto, pmsProduct);
        //由于传来的id是inteager类型的需要转化long类型的
        //Long id = Long.valueOf(productDto.getId().toString());
       // pmsProduct.setId(id);
        productMapper.insert(pmsProduct);
        //保存会员价格表
        List<PmsMemberPrice> memberPriceList = productDto.getMemberPriceList();
        for (PmsMemberPrice pmsMemberPrice : memberPriceList) {
            pmsMemberPrice.setProductId(pmsProduct.getId());
            pmsMemberPriceMapper.insert(pmsMemberPrice);
        }
        //(CmsPrefrenceAreaProductRelation)
            List<CmsPrefrenceAreaProductRelation> list = productDto.getPrefrenceAreaProductRelationList();
            for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : list) {
                cmsPrefrenceAreaProductRelation.setProductId(pmsProduct.getId());
                cmsPrefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
            }
        //保存主题产品关系表(CmsSubjectProductRelation)
        List<CmsSubjectProductRelation> subjectProductRelationList = productDto.getSubjectProductRelationList();
        for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
            cmsSubjectProductRelation.setProductId(pmsProduct.getId());
            cmsSubjectProductRelationMapper.insert(cmsSubjectProductRelation);
        }
        //保存产品属性价值表 --PmsProductAttributeValue
        List<PmsProductAttributeValue> productAttributeValueList = productDto.getProductAttributeValueList();
        for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
            pmsProductAttributeValue.setProductId(pmsProduct.getId());
            pmsProductAttributeValueMapper.insert(pmsProductAttributeValue);
        }
        //保存PmsProductFullReduction表
        List<PmsProductFullReduction> productFullReductionList = productDto.getProductFullReductionList();
        for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
            pmsProductFullReduction.setProductId(pmsProduct.getId());
            pmsProductFullReductionMapper.insert(pmsProductFullReduction);
        }
        //保存PmsProductLadder表
        List<PmsProductLadder> productLadderList = productDto.getProductLadderList();
        for (PmsProductLadder pmsProductLadder : productLadderList) {
            pmsProductLadder.setProductId(pmsProduct.getId());
            pmsProductLadderMapper.insert(pmsProductLadder);
        }
        //保存PmsSkuStock表
        List<PmsSkuStock> skuStockList = productDto.getSkuStockList();
        for (PmsSkuStock pmsSkuStock : skuStockList) {
            pmsSkuStock.setProductId(pmsProduct.getId());
            pmsSkuStockMapper.insert(pmsSkuStock);
        }
    }

    /**
     * 查询商品
     * @param brandId
     * @param keyword
     * @param productCategoryId
     * @param productSn
     * @param pageNum
     * @param pageSize
     * @param publishStatus
     * @param verifyStatus
     * @return
     */
    public PageResult list(Integer brandId,String keyword,Integer productCategoryId,String productSn,
                           Integer pageNum,Integer pageSize,Integer publishStatus,Integer verifyStatus) {
        Page page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        //商品品牌编号
        lqw.eq(brandId != null, PmsProduct::getBrandId, brandId);
        //商品名称模糊关键字
        lqw.like(!StringUtils.isEmpty(keyword), PmsProduct::getName,keyword);
        lqw.eq(productCategoryId != null, PmsProduct::getProductCategoryId, productCategoryId);
        lqw.eq(!StringUtils.isEmpty(productSn), PmsProduct::getProductSn, productSn);
        lqw.eq(publishStatus != null, PmsProduct::getPublishStatus,publishStatus);
        lqw.eq(verifyStatus != null, PmsProduct::getVerifyStatus, verifyStatus);
        lqw.eq(PmsProduct::getDeleteStatus,0);
        Page pageInfo = productMapper.selectPage(page, lqw);
        return new PageResult((int) pageInfo.getPages(), (int) pageInfo.getSize(), (int) pageInfo.getTotal(), pageInfo.getRecords());
    }

    /**
     * 根据商品名称或货号模糊查询
     *
     * @param keyword
     * @return
     */
    public List<PmsProduct> simpleList(String keyword) {
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null, PmsProduct::getKeywords, keyword);
        List<PmsProduct> list = productMapper.selectList(lqw);
        return list;
    }

    /**
     * 批量修改删除状态
     *
     * @param deleteStatus
     * @param ids
     */
    public void deleteStatus(Integer deleteStatus, List<Integer> ids) {
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null, PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setDeleteStatus(deleteStatus);
        productMapper.update(pmsProduct, lqw);

    }

    /**
     * 批量设为新品
     *
     * @param newStatus
     * @param ids
     */
    public void newStatus(Integer newStatus, List<Integer> ids) {
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null, PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setNewStatus(newStatus);
        productMapper.update(pmsProduct, lqw);
    }

    /**
     * 批量上下架商品
     *
     * @param publishStatus
     * @param ids
     */
    public void publishStatus(Integer publishStatus, List<Integer> ids) {
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null, PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setPublishStatus(publishStatus);
        productMapper.update(pmsProduct, lqw);
    }

    /**
     * 批量推荐商品
     *
     * @param recommendStatus
     * @param ids
     */
    public void recommendStatus(Integer recommendStatus, List<Integer> ids) {
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null, PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setRecommandStatus(recommendStatus);
        productMapper.update(pmsProduct, lqw);
    }

    /**
     * 批量修改审核状态
     *
     * @param detail
     * @param ids
     * @param verifyStatus
     */
    public void verifyStatus(String detail, List<Integer> ids, Integer verifyStatus) {
        //修改product表
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null, PmsProduct::getId, ids);
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setVerifyStatus(verifyStatus);
        productMapper.update(pmsProduct, lqw);
        //修改产品检验记录表
        LambdaQueryWrapper<PmsProductVertifyRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ids != null, PmsProductVertifyRecord::getProductId, ids);
        PmsProductVertifyRecord ppvr = new PmsProductVertifyRecord();
        ppvr.setDetail(detail);
        pmsProductVertifyRecordMapper.update(ppvr, wrapper);
    }

    /**
     * 更新商品
     *
     * @param id
     * @param productDto
     */
    public void updateById(Integer id, ProductDto productDto) {
        Long id1 = Long.valueOf(id.toString());
        //更新product表
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(productDto, pmsProduct);
        pmsProduct.setId(id1);
        productMapper.updateById(pmsProduct);
        //更新会员价格表
        List<PmsMemberPrice> memberPriceList = productDto.getMemberPriceList();
        for (PmsMemberPrice pmsMemberPrice : memberPriceList) {
            pmsMemberPriceMapper.updateById(pmsMemberPrice);
        }
        //更新产品属性价值表 --PmsProductAttributeValue
        List<PmsProductAttributeValue> productAttributeValueList = productDto.getProductAttributeValueList();
        for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
            pmsProductAttributeValueMapper.updateById(pmsProductAttributeValue);
        }
        //更新PmsProductFullReduction表
        //先删除原来的数据
        LambdaQueryWrapper<PmsProductFullReduction> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!StringUtils.isEmpty(id1), PmsProductFullReduction::getProductId, id1);
        pmsProductFullReductionMapper.delete(lqw);
        //再添加新来数据
        List<PmsProductFullReduction> productFullReductionList = productDto.getProductFullReductionList();
        for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
            pmsProductFullReductionMapper.insert(pmsProductFullReduction);
        }
        //更新PmsProductLadder表
        //先删除原来的数据
        LambdaQueryWrapper<PmsProductLadder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(id1), PmsProductLadder::getProductId, id1);
        pmsProductLadderMapper.delete(wrapper);
        //再添加新来数据
        List<PmsProductLadder> productLadderList = productDto.getProductLadderList();
        for (PmsProductLadder pmsProductLadder : productLadderList) {
            pmsProductLadderMapper.insert(pmsProductLadder);
        }
        //更新PmsSkuStock表
        List<PmsSkuStock> skuStockList = productDto.getSkuStockList();
        for (PmsSkuStock pmsSkuStock : skuStockList) {
            pmsSkuStockMapper.updateById(pmsSkuStock);
        }
        //更新(CmsSubjectProductRelation)
        //先删除原来数据
        LambdaQueryWrapper<CmsSubjectProductRelation> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(!StringUtils.isEmpty(id1), CmsSubjectProductRelation::getProductId, id1);
        cmsSubjectProductRelationMapper.delete(wrapper1);
        //保存新数据
        List<CmsSubjectProductRelation> subjectProductRelationList = productDto.getSubjectProductRelationList();
        for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
            cmsSubjectProductRelationMapper.insert(cmsSubjectProductRelation);
        }

        //更新(CmsPrefrenceAreaProductRelation)
        //先删除原来数据
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(!StringUtils.isEmpty(id1), CmsPrefrenceAreaProductRelation::getProductId, id1);
        cmsPrefrenceAreaProductRelationMapper.delete(wrapper2);
        //保存新数据
        List<CmsPrefrenceAreaProductRelation> list = productDto.getPrefrenceAreaProductRelationList();
        for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : list) {
            cmsPrefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
        }
    }

    /**
     * 根据商品id获取商品编辑信息
     * @param id
     * @return
     */
    public ProductDto findById(Integer id) {
        ProductDto productDto=new ProductDto();
        //查找商品信息
        Long id1=Long.valueOf(id.toString());
        PmsProduct pmsProduct = productMapper.selectById(id1);
        BeanUtils.copyProperties(pmsProduct,productDto);
        //查找memberPrice表
        LambdaQueryWrapper<PmsMemberPrice>lqw=new LambdaQueryWrapper<>();
        lqw.eq(id1!=null,PmsMemberPrice::getProductId,id1);
        List<PmsMemberPrice> pmsMemberPrices = pmsMemberPriceMapper.selectList(lqw);
        productDto.setMemberPriceList(pmsMemberPrices);
        //查找prefrenceAreaProduct表
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation>wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(id1!=null,CmsPrefrenceAreaProductRelation::getProductId,id1);
        List<CmsPrefrenceAreaProductRelation> list1 = cmsPrefrenceAreaProductRelationMapper.selectList(wrapper1);
        productDto.setPrefrenceAreaProductRelationList(list1);
        //查找PmsProductAttributeValue
        LambdaQueryWrapper<PmsProductAttributeValue>wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(id1!=null,PmsProductAttributeValue::getProductId,id1);
        List<PmsProductAttributeValue> list2 = pmsProductAttributeValueMapper.selectList(wrapper2);
        productDto.setProductAttributeValueList(list2);
        //查找PmsProductFullReduction
        LambdaQueryWrapper<PmsProductFullReduction>wrapper3=new LambdaQueryWrapper<>();
        wrapper3.eq(id1!=null,PmsProductFullReduction::getProductId,id1);
        List<PmsProductFullReduction> list3 = pmsProductFullReductionMapper.selectList(wrapper3);
        productDto.setProductFullReductionList(list3);
        //查找PmsProductLadder
        LambdaQueryWrapper<PmsProductLadder>wrapper4=new LambdaQueryWrapper<>();
        wrapper4.eq(id1!=null,PmsProductLadder::getProductId,id1);
        List<PmsProductLadder> list4 = pmsProductLadderMapper.selectList(wrapper4);
        productDto.setProductLadderList(list4);
        //查询PmsSkuStock
        LambdaQueryWrapper<PmsSkuStock>wrapper5=new LambdaQueryWrapper<>();
        wrapper5.eq(id1!=null,PmsSkuStock::getProductId,id1);
        List<PmsSkuStock> list5 = pmsSkuStockMapper.selectList(wrapper5);
        productDto.setSkuStockList(list5);
        //查询CmsSubjectProductRelation
        LambdaQueryWrapper<CmsSubjectProductRelation>wrapper6=new LambdaQueryWrapper<>();
        wrapper6.eq(id1!=null,CmsSubjectProductRelation::getProductId,id1);
        List<CmsSubjectProductRelation> list6 = cmsSubjectProductRelationMapper.selectList(wrapper6);
        productDto.setSubjectProductRelationList(list6);
        return productDto;
    }

    public PmsProduct getOne(Long productId) {
        return productMapper.selectById(productId);
    }
}
